//
//  DYAddYoukeViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/4.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit
import RxSwift
import RxDataSources
import Then

enum DYYoukeInfoType {
    case add
    case edit
}

class DYAddYoukeViewController: SYBaseTableViewController {
    
    var type: DYYoukeInfoType = .add
    var params:[String: String] = [:]
    var model: DYJQYuyuePeopleListModel!
    
    init(type: DYYoukeInfoType = .add, model: DYJQYuyuePeopleListModel? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.type = type
        self.model = model
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Constant.bgViewColor
        
        if type == .add {
            self.title = "新增游客"
        } else {
            self.title = "编辑游客"
            self.params["id"] = self.model.id
            self.params["name"] = self.model.name
            self.params["mobile"] = self.model.mobile
        }
        
        setRightNavItem()
        setTableView()
        setDataSource()
    }
    
    
    func addPeople(_ api: SYApiManager) {
        
        requestVM.baseRequest(disposeBag: dispose, type: api, modelClass: SGBaseNoPageListModel<DYJQYuyuePeopleListModel>.self) {[weak self] (res) in
            if let `self` = self {
                
                self.popVC()
            }
            
        } Error: {
            
        }
    }
    
    func setRightNavItem() {
        let item: UIBarButtonItem = UIBarButtonItem(title: "完成", style: UIBarButtonItem.Style.plain, target: self, action: #selector(rightAction))
        self.navigationItem.rightBarButtonItem=item
    }
    
    @objc func rightAction() {
        
        if let v = self.params["name"] {
            if v.isEmpty {
                self.sy_toast("请填写姓名")
                return
            }
        } else {
            self.sy_toast("请填写姓名")
            return
            
        }
        if let v = self.params["mobile"] {
            if v.isEmpty {
                self.sy_toast("请填写联系人电话")
                return
            }
            if !v.checkIsMobileNum() {
                self.sy_toast("请填写正确手机号")
                return
            }
        } else {
            self.sy_toast("请填写联系人电话")
            return
        }
//        if let v = self.params["idCard"] {
//            if v.isEmpty {
//                self.sy_toast("请填写身份证号")
//                return
//            }
//            if !IDCheck().isTrueIDNumber(text: v) {
//                self.sy_toast("请填写正确身份证号")
//                return
//            }
//        } else {
//            self.sy_toast("请填写身份证号")
//            return
//        }
        
        
        if type == .add {
            
            addPeople(.dy_jingquPeopleAdd(dic: self.params))
        } else {
            addPeople(.dy_jingquPeopleEdit(dic: self.params))
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.view.addSubview(self.tableView!)
    }
    
    func setDataSource() {
        //初始化数据
        let sections = Observable.just([
            DYAddYoukeSection(items: [DYAddYoukeModel(), DYAddYoukeModel(), DYAddYoukeModel()])
            
        ])
        
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<DYAddYoukeSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                
                if ip.row == 0 {
                    
                    let cell: DYYuyueRenCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                        as? DYYuyueRenCell ?? DYYuyueRenCell(style: .default, reuseIdentifier: "Cell")
                    cell.configureView()
                    cell.selectionStyle = .none
                    cell.nameLb.text = "姓名"
                    if self.type == .add {
                        cell.content.placeholder = "必填，输入证件上姓名"
                    } else {
                        cell.content.text = self.model.name
                    }
                    cell.passParamsClosure = {[weak self] value in
                        if let `self` = self {
                            self.params["name"] = value as? String
                        }
                    }
                    return cell
                } else if ip.row == 1 {
                    
                    let cell: DYYuyueRenCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                        as? DYYuyueRenCell ?? DYYuyueRenCell(style: .default, reuseIdentifier: "Cell")
                    cell.nameLb.text = "手机号"
                    
                    if self.type == .add {
                        cell.content.placeholder = "必填，输入联系人手机号"
                    } else {
                        cell.content.text = self.model.mobile
                    }
                    cell.selectionStyle = .none
                    cell.passParamsClosure = {[weak self] value in
                        if let `self` = self {
                            self.params["mobile"] = value as? String
                        }
                    }
                    return cell
                } else {
                    
                    let cell: DYYuyueRenCell = tv.dequeueReusableCell(withIdentifier: "Cell")
                        as? DYYuyueRenCell ?? DYYuyueRenCell(style: .default, reuseIdentifier: "Cell")
                    cell.nameLb.text = "身份证"
                    
                    if self.type == .add || String.isEmptyOrNil(self.model.idCard) {
                        cell.content.placeholder = "请输入证件号码"
                    } else {
                        cell.content.text = self.model.idCard
                    }
                    cell.selectionStyle = .none
                    cell.passParamsClosure = {[weak self] value in
                        if let `self` = self {
                            self.params["idCard"] = value as? String
                        }
                    }
                    return cell
                }
                
                
            })
        tableView.dataSource = nil
        //绑定单元格数据
        sections
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: dispose)
        
    }
    
}


//自定义Section
struct DYAddYoukeSection {
    var items: [DYAddYoukeModel]
}

extension DYAddYoukeSection : AnimatableSectionModelType {
    var identity: String {
        return ""
    }
    
    typealias Item = DYAddYoukeModel
    
    init(original: DYAddYoukeSection, items: [DYAddYoukeModel]) {
        self = original
        self.items = items
    }
}


struct DYAddYoukeModel: Equatable {
    var id: String?
}


extension DYAddYoukeModel: IdentifiableType {
    var identity: String {
        return id!
    }
    
    typealias Identity = String
}


